package com.polaris.live.viewmodel.message

import androidx.lifecycle.MutableLiveData
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.net.userService
import com.polaris.live.resp.back_resp.AgencyMemberIndicatorResp
import com.polaris.live.resp.base.ListResp
import com.polaris.live.widget.refresh.OnListListener

/**
 * FamilyMessageViewModel
 *
 * @author Created by 半仙 on 2024/6/27/027 19:55
 */
class FamilyMessageViewModel : BaseViewModel(), OnListListener {

    val mAgencyLiveData = MutableLiveData<ResultState<ListResp<AgencyMemberIndicatorResp>>>()

    private var mPageNumber = 0
    private var mPageSize = 20

    //搜索文案  如果为空就是获取列表如果不为空就是搜索接口
    private var mSearchString: String? = null
    private fun getAgencyUserList() {
        val search = mSearchString
        if (search?.isNotEmpty() == true) {
            requestLoading({ userService.searchAgencyUser(search, mPageNumber, mPageSize) }, mAgencyLiveData)
        } else {
            requestLoading({ userService.getAgencyUser(mPageNumber, mPageSize) }, mAgencyLiveData)
        }
    }

    override fun onRefresh() {
        mPageNumber = 0
        getAgencyUserList()
    }

    override fun onLoadMore(pageIndex: Int) {
        mPageNumber = pageIndex
        getAgencyUserList()
    }

    //调用这个方法表示搜索   直接赋值
    fun searchAgency(searchText: String? = null) {
        mSearchString = searchText
        onRefresh()
    }
}